<template>
  <el-row type="flex" justify="center" align="middle" class="pageWrapper">
    <el-form ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <h3>欢迎登陆</h3>
      <el-form-item label="账号">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false,
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    //重置
    resetForm() {
      this.form = {
        username: "",
        password: "",
      };
    },
    //登录
    submitForm() {
      if (this.isLoading) {
        return;
      }
      if (this.form.username == "" || this.form.password == "") {
        this.$message.warning("请输入完整用户信息");
        return;
      }
      this.$axios({
        method: "post",
        url: "/login",
        data: this.form,
      }).then((res) => {
        console.log(res.data);
        if (res.data.message == "登录成功") {
          this.isLoading = true;
          this.$message.success(res.data.message);
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", res.data.data.user.id);
          this.$router.push("/");
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.pageWrapper {
  min-height: 100vh;
  background-color: #f2f2f2;
  .el-form {
    padding: 20px 85px 0 0px;
    background-color: #fff;
    h3 {
      text-align: center;
      margin: 0 0 20px 60px;
    }
  }
}
</style>